package com.luoye.lj.util.excel;

import com.luoye.lj.util.excel.format.IEntityFormat;
import com.luoye.lj.util.excel.format.IEntityFormatExpand;
import com.luoye.lj.util.excel.format.IFormat;
import com.luoye.lj.util.excel.format.IValueFormat;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/6/27
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class ValueExtractor<T> {
    private T entity;

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public ValueExtractor(T entity) {
        this.entity = entity;
    }

    public ValueExtractor() {
    }

    public String getValue(String fieldName,IValueFormat valueFormat){
        String sValue="";
        Class<T> tClass = (Class<T>) entity.getClass();
        try {
            Field sField=tClass.getDeclaredField(fieldName);
            String tFieldType=sField.getType().getSimpleName();
            sField.setAccessible(true);
            if ("int".equals(tFieldType)) {
                try {
                    int value= sField.getInt(entity);
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            } else if ("long".equals(tFieldType)) {
                try {
                    long value= Long.parseLong(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            } else if ("float".equals(tFieldType)) {
                try {
                    float value= Float.parseFloat(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            } else if ("double".equals(tFieldType)) {
                try {
                    double value= Double.parseDouble(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            } else if ("char".equals(tFieldType)) {
                try {
                    char value=(char) Integer.parseInt(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            } else if ("byte".equals(tFieldType)) {
                try {
                    byte value= Byte.parseByte(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            } else if ("short".equals(tFieldType)) {
                try {
                    short value= Short.parseShort(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            }
            else if ("boolean".equals(tFieldType)) {
                try {
                    boolean value= Boolean.parseBoolean(sField.get(entity) + "");
                    sValue=value+"";
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (NumberFormatException ex){
                }
            }
            else if ("Date".equals(tFieldType)) {
                SimpleDateFormat sdtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date value= (Date) sField.get(entity);
                    sValue=sdtf.format(value);
                    if(valueFormat!=null){
                        sValue=valueFormat.format(value);
                    }
                }catch (Exception ex){
                }
            }else if ("String".equals(tFieldType)) {
                String value=sField.get(entity) + "";
                sValue=value;
                if(valueFormat!=null){
                    sValue=valueFormat.format(value);
                }
            }
        } catch (NoSuchFieldException e) {
        }catch (IllegalAccessException ex){
        }
        return sValue;
    }

    public String getValue(String fieldName, IEntityFormat<T> entityFormat){
        if(entityFormat!=null){
            return entityFormat.format(entity);
        }
        return getValue(fieldName,(IValueFormat)null);
    }

    public String getValue(String fieldName, IFormat format){
        if(format instanceof IEntityFormat){
            return getValue(fieldName, (IEntityFormat)format);
        }
        return getValue(fieldName,(IValueFormat)format);
    }

    public String getValue(String fieldName, IEntityFormatExpand<T> entityFormat, int i){
        if(entityFormat!=null){
            return entityFormat.format(entity,i);
        }
        return getValue(fieldName,(IValueFormat)null);
    }

    public String getValue(String fieldName,IFormat format,int i){
        if(format instanceof IEntityFormatExpand){
            return getValue(fieldName, (IEntityFormatExpand)format,i);
        }
        if(format instanceof IEntityFormat){
            return getValue(fieldName, (IEntityFormat)format);
        }
        return getValue(fieldName,(IValueFormat)format);
    }
}
